import { Body, Controller, Get, Post, UploadedFile, UploadedFiles, UseInterceptors } from '@nestjs/common';
import { FileInterceptor, FilesInterceptor } from '@nestjs/platform-express';
import { AppService } from './app.service';

@Controller()
export class AppController {
  constructor(private readonly appService: AppService) { }

  @Get('/hello')
  getHello(): string {
    return this.appService.getHello();
  }

  @Post('/upload-files')
  @UseInterceptors(FilesInterceptor('files', 3))
  uploadFileList(@UploadedFiles() files: Express.Multer.File[], @Body() body: any): ResponseData<any> {
    // 处理上传的文件
    console.log(files, body, '上传文件');
    return {
      code: 200,
      msg: '文件上传成功',
      data: files,
    }
  }

  @Post('/upload')
  @UseInterceptors(FileInterceptor('file'))
  uploadFile(@UploadedFile() file: Express.Multer.File): ResponseData<any> {
    // 处理上传的文件
    console.log(file, '上传文件');
    // 假设服务器地址为 http://localhost:3000
    const serverUrl = 'http://localhost:3000';
    const fileUrl = `${serverUrl}/uploads/${file.filename}`;
    const fileData =  {
      filename: file.filename,
      url: fileUrl,
      originalname: file.originalname,
    };
    return {
      code: 200,
      msg: '文件上传成功',
      data: fileData
    }
  }
}
